package com.dawn.module.cms.controller.app.data;

import com.dawn.framework.common.enums.CommonStatusEnum;
import com.dawn.framework.common.pojo.CommonResult;
import com.dawn.framework.common.util.object.BeanUtils;
import com.dawn.module.cms.controller.app.article.vo.AppArticlePageReqVO;
import com.dawn.module.cms.controller.app.article.vo.AppArticleRespVO;
import com.dawn.module.cms.controller.app.company.vo.AppCompanyRespVO;
import com.dawn.module.cms.controller.app.data.vo.AppConfigResponse;
import com.dawn.module.cms.controller.app.data.vo.IndexInfoResponse;
import com.dawn.module.cms.controller.app.data.vo.MineInfoResponse;
import com.dawn.module.cms.dal.dataobject.banner.CmsBannerDO;
import com.dawn.module.cms.service.article.CmsArticleService;
import com.dawn.module.cms.service.banner.CmsBannerService;
import com.dawn.module.cms.service.company.CompanyService;
import com.dawn.module.system.api.menu.MenuApi;
import com.dawn.module.system.api.menu.dto.AppMenuRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.dawn.framework.common.pojo.CommonResult.success;

@RestController
@RequestMapping("/cms")
@Tag(name = "用户 APP - 首页")
@Validated
public class AppDataController {

    @Resource
    private CmsBannerService bannerService;

    @Resource
    private CmsArticleService articleService;

    @Resource
    private CompanyService companyService;

    @Resource
    private MenuApi menuApi;

    @GetMapping("/index/data")
    @Operation(summary = "获得首页数据")
    public CommonResult<IndexInfoResponse> getIndexData() {
        IndexInfoResponse indexInfoResponse = new IndexInfoResponse();

        AppArticlePageReqVO pageReqVO = new AppArticlePageReqVO();
        pageReqVO.setPageSize(5);
        pageReqVO.setPublish(CommonStatusEnum.ENABLE.getStatus());
        pageReqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        pageReqVO.setTopping(CommonStatusEnum.ENABLE.getStatus());
        List<AppArticleRespVO> appArticleRespVOList = BeanUtils.toBean(articleService.getArticlePage(pageReqVO).getList(),AppArticleRespVO.class);
        if(appArticleRespVOList.size()>5){
            appArticleRespVOList = appArticleRespVOList.subList(0,5);
        }
        indexInfoResponse.setNewsHot(appArticleRespVOList);

        pageReqVO = new AppArticlePageReqVO();
        pageReqVO.setCategoryId(1L);
        pageReqVO.setPageNo(1);
        pageReqVO.setPageSize(5);
        String[] newsNoticeArray = new String[0];
//        PageResult<AppArticleRespVO> articleRespVOPageResult = BeanUtils.toBean(articleService.getArticlePage(pageReqVO),AppArticleRespVO.class);
        if(appArticleRespVOList.size()>0){
            newsNoticeArray = new String[appArticleRespVOList.size()];
            for (int i = 0; i < appArticleRespVOList.size(); i++) {
                AppArticleRespVO appArticleRespVO = appArticleRespVOList.get(i);
                newsNoticeArray[i] = appArticleRespVO.getTitle();
            }
        }
        indexInfoResponse.setNewsNoticeArray(newsNoticeArray);

        List<CmsBannerDO> bannerList = bannerService.getBannerListByPosition(1);
        indexInfoResponse.setBanner(bannerList);

        return success(indexInfoResponse);
    }

    @GetMapping("/mine/data")
    @Operation(summary = "获得个人中心数据")
    public CommonResult<MineInfoResponse> getMineData() {
        MineInfoResponse mineInfoResponse = new MineInfoResponse();

//        List<CmsBannerDO> bannerList = bannerService.getBannerListByPosition(2);
//        mineInfoResponse.setBanner(bannerList);

        List<CmsBannerDO> bannerDOList1 = bannerService.getBannerListByPosition(2);
        if(bannerDOList1.size()>0){
            mineInfoResponse.setMineTopBg(bannerDOList1.get(bannerDOList1.size()-1).getPicUrl());
        }

        List<CmsBannerDO> bannerDOList = bannerService.getBannerListByPosition(3);
        bannerDOList = bannerDOList.stream().filter(b -> b.getStatus() == CommonStatusEnum.ENABLE.getStatus()).collect(Collectors.toList());
        if(bannerDOList.size()>0){
            mineInfoResponse.setMainImage(bannerDOList.get(bannerDOList.size()-1).getPicUrl());
        }

        return success(mineInfoResponse);
    }

    @GetMapping("/config")
    @Operation(summary = "获得 config ")
    public CommonResult getConfig() {
        AppConfigResponse appConfigResponse = new AppConfigResponse();

        List<CmsBannerDO> bannerDOList1 = bannerService.getBannerListByPosition(2);
        if(bannerDOList1.size()>0){
            appConfigResponse.setLoginBg(bannerDOList1.get(bannerDOList1.size()-1).getPicUrl());
        }

        List<CmsBannerDO> bannerDOList = bannerService.getBannerListByPosition(3);
        bannerDOList = bannerDOList.stream().filter(b -> b.getStatus() == CommonStatusEnum.ENABLE.getStatus()).collect(Collectors.toList());
        if(bannerDOList.size()>0){
            appConfigResponse.setMineTopBg(bannerDOList.get(bannerDOList.size()-1).getPicUrl());
        }

        AppCompanyRespVO company = BeanUtils.toBean(companyService.getCompany(), AppCompanyRespVO.class);
        appConfigResponse.setCompanyRespVO(company);
        if(company.getId()!=null){
            appConfigResponse.setAppTitle(company.getName());
            appConfigResponse.setLogoUrl(company.getLogo());
        }
        Map uiMap = new HashMap();
        uiMap.put("themeColor","#02adab");
        uiMap.put("themeColorDark","#124b4b");
        uiMap.put("themeColorLight","#e9fdfc");
        appConfigResponse.setUi(uiMap);
        return success(appConfigResponse);
    }

    @GetMapping("/app/menu")
    @Operation(summary = "获取app菜单")
    @Parameter(name = "roleCode", description = "角色标识", required = true)
    public CommonResult<AppMenuRespDTO> getDictDataListByAppName(@RequestParam("roleCode") String roleCode) {
        AppMenuRespDTO appMenuResp =  menuApi.getMenuList(roleCode);
        return success(appMenuResp);
    }

}
